package org.sky.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysDatasource;
import org.sky.sys.service.SysDatasourceService;
import org.sky.sys.util.CriteriaUtils;
import org.sky.sys.util.RequestUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.util.JsonUtils;
import org.sky.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* @author weifx
* @Title: SysDatasourceController
* @ProjectName platform-core
* @Description: TODO{服务类}
* @date 2019-4-16 11:11:10
* @Version 1.0
*/
@Controller
@RequestMapping(value="/sys/SysDatasource")
public class SysDatasourceController {
    @Autowired
    private SysDatasourceService sysDatasourceService;
    /**
    * 打开页面
    * @return
    */
    @GetMapping(value="/initSysDatasourceListPage")
    public String initSysDatasourceListPage(){
        return "sys/datasource/sysdatasourcelist";
    }
    /**
    * 分页查询
    * @param request
    * @return
    */
    @PostMapping(value = "/selectSysDatasourceByPage",produces = "application/json;charset=UTF-8")
    public @ResponseBody String selectSysDatasourceByPage(HttpServletRequest request){
        String filter = request.getParameter("filter");
        Map filterMap = JsonUtils.json2map(filter);
        String sortfield=request.getParameter("sortfield");
        int pageNum=Integer.parseInt(request.getParameter("page"));//当前第几页
        int pageSize=Integer.parseInt(request.getParameter("rows"));//每页显示条数
        //组织查询条件
        Example example= new Example(SysDatasource.class);
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.initFilter(criteria, filterMap);
        if(!StringUtils.isBlank(sortfield)) {
            example.setOrderByClause(sortfield);
        }
        PageListData pageData = sysDatasourceService.selectSysDatasourceByPage(pageNum,pageSize,example);
        return JsonUtils.obj2json(pageData);
    }
    /**
    * 打开新增页面
    * @return
    */
    @GetMapping(value="/initSysDatasourceAddPage")
    public String initSysDatasourceAddPage(){
        return "sys/datasource/sysdatasourceedit";
    }
    /**
    * 打开编辑页面
    * @return
    */
    @GetMapping(value="/initSysDatasourceEditPage")
    public String initSysDatasourceEditPage(){
        return "sys/datasource/sysdatasourceedit";
    }
    /**
    * 打开明细页面
    * @return
    */
    @GetMapping(value="/initSysDatasourceDetailPage")
    public String initSysDatasourceDetailPage(){
        return "sys/datasource/sysdatasourcedetail";
    }
    /**
    * 保存{添加/修改}
    * @param request
    * @return
    */
    @PostMapping(value = "/saveAddEditSysDatasource",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysDatasource(HttpServletRequest request){
        Result result = null;
        try {
            SysDatasource sysDatasource = RequestUtils.getEntityBean(request, SysDatasource.class);
            sysDatasourceService.saveAddEditSysDatasource(sysDatasource);
            result = Result.success("保存成功",null,null);
        }catch (ServiceException e){
            result = Result.error("保存失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }
    /**
    * 根据Id查询
    * @param id
    * @return
    */
    @GetMapping(value = "/getSysDatasourceById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysDatasourceById(
        @PathVariable Long id){
        SysDatasource bean = sysDatasourceService.getSysDatasourceById(id);
        return JsonUtils.obj2json(bean);
    }
    /**
    * 根据ID删除
    * @param request
    * @return
    */
    @PostMapping(value = "/delSysDatasource",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysDatasource(HttpServletRequest request){
        Result result= null;
        try {
            String delRows=request.getParameter("delRows");
            List<SysDatasource> de=JsonUtils.json2list(delRows, SysDatasource.class);
            sysDatasourceService.delSysDatasourceById(de);
            result = Result.success("删除成功",null,null);
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            result = Result.error("删除失败",e.getMessage(),null);
        }
        return JsonUtils.obj2json(result);
    }
}